package com.tourscool.springboot.starter.log;

import com.tourscool.springboot.starter.utils.PropertySourcesUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Arrays;
import java.util.List;

/**
 * @author Cedric
 * @date 2022/07/06 17:37
 * @description 环境注入抽象类
 */
public abstract class AbstractEnvironmentPostProcessor implements EnvironmentPostProcessor {

    private static final String DEV = "dev";
    private static final String STG = "stg";
    private static final String PRD = "prod";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        PropertySourcesUtil.setEnvironment(environment);
        final List<String> profiles = Arrays.asList(environment.getActiveProfiles());
        if (profiles.contains(PRD)) {
            doPrd(environment, application);
        } else if (profiles.contains(STG)) {
            doStg(environment, application);
        } else {
            doDev(environment, application);
        }
        onProfile(environment, application);
    }

    protected void doPrd(ConfigurableEnvironment environment, SpringApplication application) {
    }

    protected void doStg(ConfigurableEnvironment environment, SpringApplication application) {
    }

    protected void doDev(ConfigurableEnvironment environment, SpringApplication application) {
    }

    protected void onProfile(ConfigurableEnvironment environment, SpringApplication application) {
    }
}
